package kassiopeia.gui

class LogsController {

    def logsService

    def index = {
        def logs = logsService.getAllLogs()
        [ logs: logs, lastLogId: getLastLogId(logs) ]
    }

    def refresh = {
        def logs
        def logId = params.logId.toLong()

        if(logId <= 0) {
            logs = logsService.getAllLogs()
        } else {
            logs = logsService.getLogsStartingFrom(logId)
        }

        render(contentType:"text/json") {
            lastLogId = getLastLogId(logs)
            content = g.render(template:"logTemplate", collection:logs)
        }
    }

    private def getLastLogId(logs) {
        if(logs.size() == 0) { return -1; }
        return logs.get(logs.size() - 1).logId
    }
}
